package us.icitap.commandcenter.utils;

import java.util.List;

import us.icitap.commandcenter.CommandCenterUI;
import us.icitap.commandcenter.business.ejb.persistence.Officer;
import us.icitap.commandcenter.business.ejb.persistence.OrgStructure;
import us.icitap.commandcenter.listeners.SearchListener;
import us.icitap.commandcenter.ui.custom.CustSearchButton;
import us.icitap.commandcenter.ui.custom.CustTable;
import us.icitap.commandcenter.ui.custom.CustTextField;

import com.vaadin.data.Property.ValueChangeEvent;
import com.vaadin.data.Property.ValueChangeListener;
import com.vaadin.data.util.BeanItemContainer;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.Window;
import com.vaadin.ui.themes.Runo;

public class SearchOfficerWindow extends Window{

	
	private final CustTable officerTable = new CustTable();
	private final CustSearchButton searchButton = new CustSearchButton("Kerko");
	private final CustTextField officerNameSurname = new CustTextField("Kerko sipas:");
	
	private SearchListener searchListener;
	
	private BeanItemContainer<us.icitap.timsportal.entity.Officer> container = new BeanItemContainer<us.icitap.timsportal.entity.Officer>(
			us.icitap.timsportal.entity.Officer.class);
	
	public SearchOfficerWindow() {
		this.center();
		this.setModal(true);
		this.setResizable(false);
		this.setCaption("Kerkoni per oficerin ne RMIS");
		
		
		HorizontalLayout hl = new HorizontalLayout();
		this.officerNameSurname.setWidth("200px");
		this.officerNameSurname.focus();
		officerNameSurname.setInputPrompt("Emer Mbiemer");
		hl.addComponent(officerNameSurname);
		hl.addComponent(searchButton);
		hl.setComponentAlignment(searchButton, Alignment.BOTTOM_RIGHT);
		VerticalLayout vl = new VerticalLayout();
		vl.setMargin(true);
		vl.addComponent(hl);
		officerTable.setContainerDataSource(container);
		officerTable.setStyleName(Runo.TABLE_SMALL);
		officerTable.setWidth("98%");
		officerTable.setDescription("Per te zgjedhur nje oficer nga tabela duhet te klikoni rreshtin");
		
		officerTable.setPageLength(10);
		officerTable.setSelectable(true);
		officerTable.setImmediate(true);
		
		
		officerTable.setVisibleColumns(new Object[]{"officerId","forename", "surname", "function"});
		officerTable.setColumnHeaders(new String[]{"ID", "EMRI", "MBIEMRI", "FUNKSIONI"});
		
//		loadOfficerTable(null);
		vl.addComponent(officerTable);
		vl.setSpacing(true);
		this.setContent(vl);
		this.setWidth("700px");
		this.setHeight("400px");
		this.setPositionX(30);
		this.setPositionY(5);

		this.addCloseListener(new CloseListener() {
			@Override
			public void windowClose(CloseEvent e) {
				CommandCenterUI.getCurrent().removeWindow(SearchOfficerWindow.this);
				officerNameSurname.setValue(null);
//				officerTable.setContainerDataSource(null);
				container.removeAllItems();
			}
		});
		
		officerTable.addValueChangeListener(new ValueChangeListener() {			
			@Override
			public void valueChange(ValueChangeEvent event) {

				us.icitap.timsportal.entity.Officer selectedOfficer = (us.icitap.timsportal.entity.Officer) event.getProperty().getValue();
				
				
				if (selectedOfficer == null)
					return;
				//vendos strukturen te officer ne baze te modulit
				us.icitap.timsportal.entity.OrgStructure baseOs = null;

				baseOs = TPUtil.portalBeanRemote
							.getOfficerBaseStructureByModule(
									selectedOfficer, 6);
				if(baseOs==null){HandleNotification.showWarningOnScreen("Ky oficer nuk ka strukture baze ne modul");return;}
				
				Officer officer = new Officer(selectedOfficer.getOfficerId(), selectedOfficer.getForename().toUpperCase() + " " + selectedOfficer.getSurname().toUpperCase());
				officer.setOrgStructure(new OrgStructure(baseOs.getId(), baseOs.getName())); 
				
				searchListener.searchFinished(officer);
				CommandCenterUI.getCurrent().removeWindow(SearchOfficerWindow.this);
					
			}
		});
		
		searchButton.addClickListener(new ClickListener() {
			
			@Override
			public void buttonClick(ClickEvent event) {
	
				if (  officerNameSurname.getValue() != null
						&& !officerNameSurname.getValue().toString()
								.trim().equals("")) {
					String[] nameSurname = officerNameSurname.getValue()
							.toString().split(" ");
					if (nameSurname.length > 1) {
						container.removeAllItems();
						
						List<us.icitap.timsportal.entity.Officer> officerList = TPUtil.portalBeanRemote.getRmisOfficerByName(nameSurname[0],nameSurname[1]);
						System.err.println(officerList);
						if (officerList != null){
//							container.removeAllItems();
							container.addAll(officerList);
						}
						else
							HandleNotification.showInfoOnTray("Nuk ka asnje rezultat nga kerkimi");
		
					} else 
						HandleNotification.showWarningOnScreen("Duhet te jepni emer dhe mbiemer dhe ti ndani me hapesire.");
					
				}
				
				
				
				
				
				
				
				
			}
		});
		
	}

	
	public void addSearchListener(SearchListener searchListener){
		this.searchListener = searchListener;
	}
}
